package org.learn.spring_quartz.lib;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.calendar.HolidayCalendar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

@Component
public class MyHolidayCalendar {

    private final Scheduler scheduler;

    @Autowired
    public MyHolidayCalendar(Scheduler scheduler) {
        this.scheduler = scheduler;
    }

    public void addCalendar() throws SchedulerException {
        HolidayCalendar holidayCalendar = new HolidayCalendar();

        // 定义节假日（例如 2025-08-29）
        LocalDate holiday1 = LocalDate.of(2025, 8, 30);
        Date date1 = Date.from(holiday1.atStartOfDay(ZoneId.systemDefault()).toInstant());
        holidayCalendar.addExcludedDate(date1);

        // 继续添加其他节假日
        LocalDate holiday2 = LocalDate.of(2025, 12, 25);
        holidayCalendar.addExcludedDate(Date.from(holiday2.atStartOfDay(ZoneId.systemDefault()).toInstant()));

        // 注册到 Quartz Scheduler
        scheduler.addCalendar("holidays", holidayCalendar, true, true);
    }
}
